use crate::export;
use cortex_m::{asm::bootload, interrupt::free};

pub unsafe fn boot2(vtor_addr: u32) -> ! {
    free(|_| unsafe {
        let nvic: export::device::NVIC = core::mem::transmute(());
        for reg in nvic.icer.iter() {
            reg.write(0xFFFFFFFF);
        }
        for reg in nvic.icpr.iter() {
            reg.write(0xFFFFFFFF);
        }
        let rcc = export::device::RCC::steal();
        rcc.ahb1rstr().write(|w| w.bits(0xFFFFFFFF));
        rcc.ahb2rstr().write(|w| w.bits(0xFFFFFFFF));
        rcc.ahb3rstr().write(|w| w.bits(0xFFFFFFFF));
        rcc.apb1rstr().write(|w| w.bits(0xFFFFFFFF));
        rcc.apb2rstr().write(|w| w.bits(0xFFFFFFFF));

        rcc.ahb1rstr().write(|w| w.bits(0));
        rcc.ahb2rstr().write(|w| w.bits(0));
        rcc.ahb3rstr().write(|w| w.bits(0));
        rcc.apb1rstr().write(|w| w.bits(0));
        rcc.apb2rstr().write(|w| w.bits(0));
    });
    unsafe {
        bootload(vtor_addr as _);
    }
}


